/**
 * 
 */
package com.maszy.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.maszy.commons.tools.PrintGoodPlanReport2;
import com.maszy.commons.tools.PrintGoodPlanReport3;
import com.maszy.commons.tools.PrintTableTest;
import com.maszy.commons.tools.PrintTableTest2;

/**
 * @作者 Allen Shu
 * @创建日期 2012-6-20
 * @版本 V 1.0
 */

public class PrintRunnable implements Runnable {

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		// TODO Auto-generated method stub
		DateFormat df = new SimpleDateFormat("HH:mm");
		DateFormat df2 = new SimpleDateFormat("HH:mm:ss");
		try{
			// 原材料供应单打印时间
			Date print1 = df.parse("13:15");//13:15
			// 次日补单打印时间
			Date print2 = df.parse("4:00");   // 4:00 
		 	// 当前系统时间
			//早点打印单 
			Date print3=df.parse("12:00");//12:00
			Date now=new Date();
			String nowTime=df2.format(now);
			Date serverTime=df2.parse(nowTime);
			long linchen = print2.getTime()-serverTime.getTime() ;
			long xiawu=print1.getTime()-serverTime.getTime();
			long zaodian=print3.getTime()-serverTime.getTime();
			System.out.println("------------------------------------------------------------------------------------------------");
			if(linchen>0){
				System.out.println(""+(linchen/1000/60)+"分钟后，系统即将打印次日补货单.");
				 System.out.println("凌晨:"+linchen+"\t早点:"+zaodian+"\t下午:"+xiawu+"\t"+"\t服务器时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
				Thread.sleep(print2.getTime()-df2.parse((df2.format(new Date()))).getTime());
				run();
			}else if(linchen==0){
//				PrintGoodPlanReport2 p2 = new PrintGoodPlanReport2();
//				p2.PrintReady("3");
//				 PrintTableTest2 pt2=new PrintTableTest2();
//				 pt2.printReport2("3");
				 
				 System.out.println("开始打印次日补货单");
				 System.out.println("凌晨:"+linchen+"\t早点:"+zaodian+"\t下午:"+xiawu+"\t"+"\t服务器时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
				 PrintTableTest pt1=new PrintTableTest();
				 pt1.printReport1();
				 System.out.println(zaodian/1000/60+"分钟后开始打印早点汇总单.");
				 Thread.sleep(print3.getTime()-df2.parse((df2.format(new Date()))).getTime());
				 run();
			}else if(zaodian>0){
				System.out.println(""+(zaodian/1000/60)+"分钟后，系统即将打印早点汇总单.");
				System.out.println("凌晨:"+linchen+"\t早点:"+zaodian+"\t下午:"+xiawu+"\t"+"\t服务器时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
				Thread.sleep(print3.getTime()-df2.parse((df2.format(new Date()))).getTime());
				run();
			}else if(zaodian==0){
				System.out.println("开始打印早点汇总单");
				System.out.println("凌晨:"+linchen+"\t早点:"+zaodian+"\t下午:"+xiawu+"\t"+"\t服务器时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
				PrintGoodPlanReport3 ptool = new PrintGoodPlanReport3();
				ptool.printContent();
				 System.out.println(xiawu/1000/60+"分钟后系统即将打印原材料供应单和下午补货单.");
				Thread.sleep(print1.getTime()-df2.parse((df2.format(new Date()))).getTime());
		    	 run();
			}
			else if(xiawu>0){
				System.out.println(""+(xiawu/1000/60)+"分钟后，系统即将打印原材料供应单和下午补货单.");
				System.out.println("凌晨:"+linchen+"\t早点:"+zaodian+"\t下午:"+xiawu+"\t"+"\t服务器时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
				Thread.sleep(print1.getTime()-df2.parse((df2.format(new Date()))).getTime());
				run();
			}			
			else if(xiawu==0){
				System.out.println("开始打印原材料供应单和下午补货单");
				 System.out.println("凌晨:"+linchen+"\t早点:"+zaodian+"\t下午:"+xiawu+"\t"+"\t服务器时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
				 PrintTableTest2 pt2=new PrintTableTest2();
				 pt2.printReport2("2");
		    	 System.out.println((24*60*60*1000+(print2.getTime()-serverTime.getTime()))/1000/60+"分钟后系统即将打印次日补货单.");
				 Thread.sleep((24*60*60*1000+(print2.getTime()-df2.parse((df2.format(new Date()))).getTime())));
				 run();
			}else{
				Thread.sleep((24*60*60*1000+(print2.getTime()-df2.parse((df2.format(new Date()))).getTime())));
				System.out.println("今日已打完等后明日打印次日补货单");
				
				run();
			}
			
		   
		}catch (Exception e){
		}
	}

}
